package com.cuit.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.mapper.BookKeepMapper;
import com.cuit.pojo.Book;
import com.cuit.pojo.BookKeep;
import com.cuit.pojo.BookUser;
import com.cuit.result.R;
import com.cuit.service.BookKeepService;
import com.cuit.service.BookService;
import com.cuit.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@CrossOrigin //开放前端的跨域访问
@RestController
@RequestMapping("/bookKeep")
public class BookKeepController {
    @Resource
    private BookKeepService bookKeepService;

    @Autowired
    private BookService bookService;

    @Resource
    private BookKeepMapper bookKeepMapper;

    @Resource
    private FileService fileService;

    @GetMapping("/list/{userId}")
    @ApiOperation("获得所有图书测试接口")
    public R getAllBook(@PathVariable Integer userId){

        QueryWrapper<BookKeep> queryWrapper= new QueryWrapper();
        queryWrapper.like("userId",userId);
        List<BookKeep> bookKeep= bookKeepMapper.selectList(queryWrapper);
        bookKeep.forEach(bookKeepList -> {
            bookKeepList.setBookImg(fileService.allPath(bookKeepList.getBookImg()));
        });
        return R.ok().data("bookList",bookKeep);
//        List<BookKeep> list = bookKeepService.list();
//        System.out.println(list);
//        list.forEach(bookKeep -> {
//            bookKeep.setBookImg(fileService.allPath(bookKeep.getBookImg()));
//        });
//        return R.ok().data("bookList",list);
    }

    @PostMapping("/list")
    @ApiOperation("根据名字搜索图书")
    public R getBookByName(@RequestBody String bookName){
        JSONObject jsonObject=JSON.parseObject(bookName);
        HashMap<String, Object> map = JSON.parseObject(String.valueOf(jsonObject), new TypeReference<HashMap<String, Object>>() {});
        String bookName2=map.get("bookName").toString();
        Integer userId=(Integer) map.get("userId");

        System.out.println(map.get("bookName"));
        System.out.println(map.get("userId"));
        System.out.println(jsonObject);
        QueryWrapper<BookKeep> queryWrapper= new QueryWrapper();
        queryWrapper.like("bookname",bookName2).like("userId",userId);

        List<BookKeep> bookKeep= bookKeepMapper.selectList(queryWrapper);
        bookKeep.forEach(bookKeepList -> {
            bookKeepList.setBookImg(fileService.allPath(bookKeepList.getBookImg()));
        });
        return R.ok().data("bookKeepList",bookKeep);
//        List<BookKeep> list = new ArrayList<>();
//        if(bookKeep != null) {
//            bookKeep.setBookImg(fileService.allPath(bookKeep.getBookImg()));
//            list.add(bookKeep);
//            return R.ok().data("bookList",list);
//        }else {
//            return R.error();
//        }
    }

    @PostMapping("/delete")
    @ApiOperation("删除图书")
    public R deleteById(@RequestBody String id){
        JSONObject jsonObject=JSON.parseObject(id);
        HashMap<String, Object> map = JSON.parseObject(String.valueOf(jsonObject), new TypeReference<HashMap<String, Object>>() {});
        Integer bookId=(Integer) map.get("bookId");
        Integer userId=(Integer) map.get("userId");
        System.out.println(id);
        System.out.println(bookId);
        System.out.println(userId);
        int delete=bookKeepMapper.deleteById(bookId);
        if(delete!=0){
            return getAllBook(userId);
        }else{
            return R.error();
        }
    }

    @PostMapping("/save")
    @ApiOperation("发布保存的图书")
    public R save(@RequestBody String body){
        JSONObject jsonObject=JSON.parseObject(body);
        HashMap<String, Object> map = JSON.parseObject(String.valueOf(jsonObject), new TypeReference<HashMap<String, Object>>() {});
        String bookImg=map.get("bookImg").toString();
        String str="http://47.109.38.52:80/";
        String bookImg2=bookImg.replace(str,"");
        Integer bookPrice=Integer.valueOf(map.get("bookPrice").toString());
        String bookName=map.get("bookName").toString();
        String bookDesc=map.get("bookDesc").toString();
        Integer bookId=(Integer) map.get("bookId");
        Integer userId=(Integer)map.get("userId");
        System.out.println(bookImg2);
        System.out.println(bookPrice);
        System.out.println(bookName);
        System.out.println(bookDesc);
        System.out.println(bookId);
        System.out.println(userId);
        System.out.println(jsonObject);
        Book book=new Book();
        book.setBookimg(bookImg2);
        book.setSellnum(30);
        book.setBookdesc(bookDesc);
        book.setBookname(bookName);
        book.setBookprice(bookPrice);
        book.setBooktypename("科幻类");
        book.setUserid(userId);
        int delete=bookKeepMapper.deleteById(bookId);
        boolean save=bookService.save(book);
        if(save){
            return getAllBook(userId);
        }else{
            return R.error();
        }
    }
}
